const query = require('../utils/query')

// 获取所有用户
const getUserList = (...rest) => {
  const sql = 'select * from cms_user'
  return query(sql, ...rest)
}

// 根据用户id获取用户信息
const getUserInfo = (id) => {
  const sql = 'select * from cms_user where id = ?'
  return query(sql, id)
}

// 根据 username 查询用户
const userIsExistByUsername = (username) => {
  const sql = 'select * from cms_user where username = ?'
  return query(sql, username)
}

// 新增用户
const insertUser = (username, password) => {
  const sql =
    'insert cms_user (username, password, user_status) values (?, ?, 1)'
  return query(sql, username, password)
}

// 根据id查询用户信息
const queryUserById = (id) => {
  const sql = 'select * from cms_user where id = ?'
  return query(sql, id)
}

// 根据 username 和 password 查询用户信息
const selectUserByUsernamePassword = (username, password) => {
  const sql =
    'select id, username, gender, role, user_status, create_time, avatar from cms_user where username = ? and password = ?'
  return query(sql, username, password)
}

// 更新用户 avatar
const updateUserAvatar = (id, url) => {
  const sql = 'update cms_user set avatar = ? where id = ?'
  return query(sql, url, id)
}

module.exports = {
  getUserList,
  getUserInfo,
  userIsExistByUsername,
  insertUser,
  queryUserById,
  selectUserByUsernamePassword,
  updateUserAvatar
}
